package com.tanhua.dubbo.api;

import com.tanhua.api.MovementApi;
import com.tanhua.dubbo.util.IdWorker;
import com.tanhua.model.mongo.Movement;
import com.tanhua.model.mongo.MovementTimeLine;
import com.tanhua.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.data.web.SpringDataWebProperties;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;

import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


@DubboService
public class MovementApiImpl implements MovementApi {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private TimeLineService timeLineService;
    @Override
    public void publish(Movement movement) {
        movement.setPid(idWorker.getNextId("movement"));
        movement.setCreated(System.currentTimeMillis());
        mongoTemplate.save(movement);
       timeLineService.saveTimeLine(movement);

    }

    @Override
    public PageResult<Movement> findPageByUserId(Long userId, int page, int pageSize) {
        Query query = new Query(Criteria.where("userId").is(userId));
        long count = mongoTemplate.count(query, Movement.class);
query.skip((page - 1)*pageSize).limit(pageSize).with(Sort.by(Sort.Order.desc("created")));

        //4 查询当前页的记录，根据分页查询
        List<Movement> movementList = mongoTemplate.find(query, Movement.class);

        //5 返回结果
        return new PageResult<>(page, pageSize, (int) count, movementList);
    }

    @Override
    public List<Movement> findMovements(int page, int pageSize, Long getid) {

        Query query = new Query(Criteria.where("friendId").is(getid));

        List<MovementTimeLine> timeLineList = mongoTemplate.find(query, MovementTimeLine.class);
        List<ObjectId> collect = timeLineList.stream().map(MovementTimeLine::getMovementId).collect(Collectors.toList());
        Query query1 = new Query(Criteria.where("id").in(collect));
        query1.skip((page - 1)*pageSize)
                .limit(pageSize).with(Sort.by(Sort.Order.desc("created")));
        List<Movement> movements = mongoTemplate.find(query1, Movement.class);

        return movements;


    }

    @Override
    public List<Movement> radomMovements(int pageSize) {
        TypedAggregation aggregation = Aggregation.newAggregation(Movement.class,Aggregation.sample(pageSize));
        AggregationResults<Movement> aggregate = mongoTemplate.aggregate(aggregation, Movement.class);

        return aggregate.getMappedResults();
    }

    @Override
    public List<Movement> findMovementsByPids(List<Long> pids) {
        Query query = Query.query(Criteria.where("pid").in(pids));
        List<Movement> list = mongoTemplate.find(query, Movement.class);
        return list;
    }

    @Override
    public Movement findOneByid(String id) {
        Movement movement = mongoTemplate.findById(new ObjectId(id), Movement.class);
        return movement;
    }
    //查询动态
    @Override
    public PageResult findMovementsByUid(Integer page, Integer pagesize, Long uid, Integer state) {
        Criteria criteria =Criteria.where("userId").is(uid);
        if (state !=null){
            criteria.and("state").is(state);
        }
        Query query = Query.query(criteria);
        long count = mongoTemplate.count(query, Movement.class);

        query.skip((page - 1)*pagesize).limit(pagesize).with(Sort.by(Sort.Order.desc("created")));

        List<Movement> movementList = mongoTemplate.find(query, Movement.class);

        return  new  PageResult(page,pagesize, (int) count,movementList);
    }
}
